package com.woniuxy.commons.config.swagger;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * <P>Author ideaAdmin
 * <P>TODO: wagger2配置信息
 *          swagger 测试地址: http://localhost:9000/swagger-ui.html
 *          Api Documentation 测试地址: http://localhost:9000/doc.html#
 *
 * <P>Date 11:29 2022/4/19
 * <P>* @param null
 * <P>* @return
 **/
@Configuration
@EnableSwagger2 //开启swagger2操作 需要在自己的启动类上开启
//该注解是knife4j提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能,如果你想使用这些增强功能就必须加该注解，否则可以不用加
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    /**
     * 参考文档Demo
     * @return
     */
    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 人人开源默认的接口
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                //   .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
                .apis(RequestHandlerSelectors.basePackage(StrUtil.format("io.renren.modules.sys.controller")))
                .paths(PathSelectors.any())
                .build()
                ;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("黄金项目ERP")
                .description("renren-fast文档")
                .termsOfServiceUrl("https://www.renren.io")
                .version("3.0.0")
                .build();
    }





    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示  路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/.*")))
                .build();

    }


    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("woniuxy", "http://www.baidu.com", ""))
                .build();
    }


}
